/*
 * Created on 13/Dez/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package libjdc.dc.io;
import libjdc.dc.protocol.Protocol;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;

/**
 * @author botelhodaniel
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DCBufferedReader extends BufferedReader {
    
    public DCBufferedReader(InputStreamReader in) {
        super(in);
    }
    
    public String readMessage() throws IOException{
        String buffer = "";

        int ch;
        while (!buffer.endsWith(Protocol.COMMAND_END_CHAR+""))
            if(((ch = read()) == -1))
                throw new IOException("Stream is Closed");
            else
                buffer += (char)ch;
        return buffer;
    }
    
    public String readMessage(char delim) throws IOException{
        String buffer = "";

        int ch;
        while (!buffer.endsWith(delim+""))
            if(((ch = read()) == -1))
                throw new IOException("Stream is Closed");
            else
                buffer += (char)ch;
        System.out.println(buffer);
        return buffer;
    }
    
    public String read(int size,char delim) throws IOException{
        CharBuffer buffer = CharBuffer.allocate(size);
        
        int ch;
        while(((ch = read()) != -1)){
            System.out.print((char)ch);
            buffer.append((char)ch);
        }
        System.out.println(buffer.toString());
        return buffer.toString();
    }
    
    public void sniff() throws IOException{
        
        
        int ch;
        while(((ch = read()) != -1))
            System.out.print((char)ch);
        throw new IOException("Stream is Closed");
        
    }
    public char[] readBytes(RemoteFile remoteFile) throws IOException{

        System.out.println("size:"+remoteFile.getLength());
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (remoteFile.getLength() > Integer.MAX_VALUE) {
            // File is too large
            throw new IOException("File is too large ");
        }
    
        // Create the byte array to hold the data
        char[] bytes = new char[(int)remoteFile.getLength()];
    
        // Read in the bytes
        //int offset = 0;
        int numRead = 0;
        while (remoteFile.getOffset() < bytes.length
               && (numRead = read(bytes, remoteFile.getOffset(), bytes.length-remoteFile.getOffset())) >= 0) {
            //offset += numRead;
            remoteFile.setOffset(remoteFile.getOffset()+numRead);
            
        }
        System.out.println("Ficheiro lido:"+bytes);
    
        // Ensure all the bytes have been read in
        if (remoteFile.getOffset() < bytes.length) {
            throw new IOException("Could not completely read file ");
        }

        return bytes;
    }
    
    public char[] readBytes(int size) throws IOException{

        System.out.println("size:"+size);
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (size > Integer.MAX_VALUE) {
            // File is too large
            throw new IOException("File is too large ");
        }
    
        // Create the byte array to hold the data
        char[] bytes = new char[(int)size];
    
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead = read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        System.out.println("Ficheiro lido:"+bytes);
    
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file ");
        }

        return bytes;
    }

}
